package cool.zhoujie.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import cool.zhoujie.Constant;

public class PlayService extends Service implements PlayController{

    private PlayBinder playBinder = new PlayBinder();

    public MediaPlayer mediaPlayer;

    public int bufferPercent;

    private String TAG = Constant.TAG_SERVICE;

    InputStream is = null;
    HttpURLConnection connection = null;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer = new MediaPlayer();
        Log.d(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return playBinder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public void initMediaPlayer(String songPath) {
        mediaPlayer.reset();
        try {
            if (songPath != null) {
//                if (checkFile(songPath)) {
//
//                }
                mediaPlayer.setDataSource(songPath);
                Log.d(Constant.TAG_PLAY, "播放器初始化开始");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean checkFile(final String address) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    is = connection.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (is != null) {
                            is.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
        return is != null;
    }

    private void setListener() {
//        当mediaPlayer准备好回放时调用
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                Log.d(Constant.TAG_PLAY, "准备完成，播放进行中");
            }
        });
//        指示网络媒体资源缓冲状态
        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.d(Constant.TAG_PLAY, "onBufferingUpdate: ");
                bufferPercent = percent;
            }
        });
//        当mediaPlayer回放完成时调用
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //默认单曲循环
                Log.d(Constant.TAG_PLAY, "歌曲播放完成");
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(Constant.TAG_PLAY, "onError: " + what + extra);
                return false;
            }
        });
    }

    @Override
    public void play() {
        setListener();
        mediaPlayer.prepareAsync();
        mediaPlayer.setVolume(1, 1);
        Log.d(Constant.TAG_PLAY, "异步准备开始");
    }

    @Override
    public void pause() {
        mediaPlayer.pause();
    }

    @Override
    public void previous() {

    }

    @Override
    public void next() {

    }

    @Override
    public void singleCycle() {

    }

    @Override
    public void listCycle() {

    }

    @Override
    public void orderPlay() {

    }

    @Override
    public void randomPlay() {

    }

    public class PlayBinder extends Binder {
//        返回service对象
        public PlayService getService() {
            return PlayService.this;
        }
    }
}
